package com.gofun.framework.core.exception.support;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gofun.framework.core.exception.BadRequestException;
import com.gofun.framework.core.exception.ForbiddenException;
import com.gofun.framework.core.exception.InternalServerException;
import com.gofun.framework.core.exception.UnauthorizedException;


@ControllerAdvice
public class GlobalExceptionHandler {
	
	/**
	 *  @Description	: 处理全局BadRequestException异常
	 *  @return         : ErrorInfo<String>
	 *  @Creation Date  : 2016年5月3日 下午8:17:07 
	 *  @Author         : wangchao
	 */
	@ExceptionHandler(value = BadRequestException.class)
    @ResponseBody
    public ErrorInfo<String> exceptionHandler(HttpServletRequest req,HttpServletResponse response, BadRequestException e) throws Exception {
		responseHandler(response, HttpStatus.BAD_REQUEST);
		return buildErrorInfo(HttpStatus.BAD_REQUEST, e, req);
    }
	
	/**
	 *  @Description	: 处理全局InternalServerException异常
	 *  @return         : ErrorInfo<String>
	 *  @Creation Date  : 2016年5月3日 下午8:15:10 
	 *  @Author         : wangchao
	 */
	@ExceptionHandler(value = InternalServerException.class)
    @ResponseBody
    public ErrorInfo<String> exceptionHandler(HttpServletRequest req,HttpServletResponse response, InternalServerException e) throws Exception {
        responseHandler(response, HttpStatus.INTERNAL_SERVER_ERROR);
		return buildErrorInfo(HttpStatus.INTERNAL_SERVER_ERROR, e, req);
    }
	
	/**
	 *  @Description	: 处理全局ForbiddenException异常
	 *  @return         : ErrorInfo<String>
	 *  @Creation Date  : 2016年5月3日 下午8:15:10 
	 *  @Author         : wangchao
	 */
	@ExceptionHandler(value = ForbiddenException.class)
    @ResponseBody
    public ErrorInfo<String> exceptionHandler(HttpServletRequest req,HttpServletResponse response, ForbiddenException e) throws Exception {
        responseHandler(response, HttpStatus.FORBIDDEN);
		return buildErrorInfo(HttpStatus.FORBIDDEN, e, req);
    }
	
	/**
	 *  @Description	: 处理全局UnauthorizedException异常
	 *  @return         : ErrorInfo<String>
	 *  @Creation Date  : 2016年5月3日 下午8:18:59 
	 *  @Author         : wangchao
	 */
	@ExceptionHandler(value = UnauthorizedException.class)
    @ResponseBody
    public ErrorInfo<String> exceptionHandler(HttpServletRequest req,HttpServletResponse response, UnauthorizedException e) throws Exception {
        responseHandler(response, HttpStatus.UNAUTHORIZED);
		return buildErrorInfo(HttpStatus.UNAUTHORIZED, e, req);
    }
	
  // /**
  // *
  // * @Description : 处理上传文件时超出最大限制的异常
  // * @return : ErrorInfo<String>
  // * @Creation Date : 2016年5月27日 下午4:25:39
  // * @Author : chutong
  // */
  // @ExceptionHandler(value = MultipartException.class)
  // @ResponseBody
  // public ErrorInfo<String> exceptionHandler(HttpServletRequest req, HttpServletResponse response,
  // MultipartException e) throws Exception {
  // responseHandler(response, HttpStatus.BAD_REQUEST);
  // String maxFileSize = PropertyUtils.getProperty("multipart.maxFileSize");
  // ErrorInfo<String> info =
  // new ErrorInfo<>(HttpStatus.BAD_REQUEST.value(), "上传文件大小超过最大限制:"+maxFileSize);
  // if (req != null)
  // info.setUrl(req.getRequestURL().toString());
  // return info;
  // }
	
	/**
	 *  @Description	: 处理全局Exception异常
	 *  @return         : ErrorInfo<String>
	 *  @Creation Date  : 2016年5月3日 下午8:20:01 
	 *  @Author         : wangchao
	 */
	@ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ErrorInfo<String> exceptionHandler(HttpServletRequest req,HttpServletResponse response, Exception e) throws Exception {
        responseHandler(response, HttpStatus.INTERNAL_SERVER_ERROR);
		return buildErrorInfo(HttpStatus.INTERNAL_SERVER_ERROR, "内部服务异常", req);
    }
	
	/**
	 *  @Description	: 设置http 响应状态
	 *  @return         : void
	 *  @Creation Date  : 2016年5月3日 下午8:14:27 
	 *  @Author         : wangchao
	 */
	private void responseHandler(HttpServletResponse response,HttpStatus status){
		if(response != null)
			response.setStatus(status.value());
	}
	
	/**
	 *  @Description	: 构建异常消息体
	 *  @return         : ErrorInfo<String>
	 *  @Creation Date  : 2016年5月3日 下午8:14:43 
	 *  @Author         : wangchao
	 */
	private ErrorInfo<String> buildErrorInfo(HttpStatus code,Exception e,HttpServletRequest req){
		return buildErrorInfo(code, e.getMessage(), req);
	}
	
	/**
	 *  @Description	: 构建异常消息体
	 *  @return         : ErrorInfo<String>
	 *  @Creation Date  : 2016年5月3日 下午8:14:43 
	 *  @Author         : wangchao
	 */
	private ErrorInfo<String> buildErrorInfo(HttpStatus code,String message,HttpServletRequest req){
		ErrorInfo<String> info = new ErrorInfo<>(code.value(),message);
		if(req != null)
			info.setUrl(req.getRequestURL().toString());
		return info;
	}
	

}
